﻿@{
    Layout = "~/Views/Shared/_Grid.cshtml";
    ViewBag.Title = "用户管理";
}
<div id="datagrid"></div>
<script type="text/javascript">
    var model;//缓存变量
    $(function () {
        //定义数据列
        getSource('UserId',
            [
                { name: 'UserId', type: 'number' },
                { name: 'UserType', type: 'number' },
                { name: 'UserName', type: 'string' },
                { name: 'CreateTime', type: 'date' },
                { name: 'Status', type: 'number' },
            ], '/sys/user/ajaxget');
        getAdapter();//适配器
        //渲染
        getGridData(
            function (toolBar) {
                var container = $("<div style='overflow: hidden; position: relative;width: 100%;'></div>");
                var input = $("<div style='float:left;line-height:20px;padding:3px;'>\
                                    <span><input class='jqx-input jqx-widget-content jqx-rc-all' id='key' type='text' placeholder='关键字 ...' style='height: 25px; width: 200px;padding-left:5px;' /></span>\
                                    <span style='width:55px;' align='center'><button id='btn-get' class='ui button xs blue'><i class='search icon'></i> 查询</button></span>\
                                </div>");
                var btnop = $("<div style='float:right;line-height:20px;padding:3px;'>\
                                    <button id='btn-add' class='ui button xs green'><i class='plus icon'></i>新增</button>\
                                    <button id='btn-edit' class='ui button xs teal'><i class='edit icon'></i>编辑</button>\
                                    <button id='btn-del' class='ui button xs red'><i class='trash outline icon'></i>删除</button>\
                                </div>");
                container.append(input);
                container.append(btnop);
                toolBar.append(container);
            },
            function () {
                $("#btn-get").on('click', function (event) {
                    search()
                });
                $('#key').bind('keyup', function (event) {
                    if (event.keyCode == 13) {
                        search()
                    }
                });
                $("#btn-add").on('click', function (event) {
                    editBox(false);
                });

                $("#btn-edit").on('click', function (event) {
                    editBox(true);
                });

                $("#btn-del").on('click', function (event) {
                    del()
                });

                $("#datagrid").on('rowDoubleClick', function (event) {
                    editBox(true);
                });
            },
            [
              { text: '序号', dataField: "UserId", align: 'center', cellsAlign: 'center' },
              { text: '用户类型', dataField: "UserType", align: 'center', cellsAlign: 'center', cellsRenderer: function (row, column, value) { if (value == 2) { return "管理员" }}},
              { text: '用户名', dataField: "UserName", align: 'center', cellsAlign: 'center' },
              { text: '创建时间', dataField: "CreateTime", cellsFormat: 'yyyy-MM-dd HH:mm:ss', align: 'center', cellsAlign: 'center' },
              { text: '状态', dataField: "Status", align: 'center', cellsAlign: 'center', cellsRenderer: function (row, column, value) { if (value == 1) { return "正常" }}}
            ]);
    });
    //查询
    function search() {
        //alert(1);
    }

    function editBox(flag) {
        model = $("#datagrid").jqxTreeGrid('getSelection')[0];
        if (flag) {
            if (model == undefined) {
                dlg.Error('对不起，请选中您要操作的记录！');
                return;
            }
        }
        else {
            model = undefined;//置空
        }
        var arrHtml = [];
        arrHtml.push('<table class="dialogTb">');
        arrHtml.push('    <tr>');
        arrHtml.push('        <td><div class="ui mini input"><input type="text"></div></td>');
        arrHtml.push('    </tr>');
        arrHtml.push('</table>');
        arrHtml.push('<div class="dialog_bottom">');
        arrHtml.push('    <button onclick="javascript:FnChange();" class="ui blue button" title="确定"><i class="check icon"></i>提交</button>');
        arrHtml.push('    <button onclick="javascript:dlg.cls(\'dlg-name\')" class="ui button" title="取消"><i class="remove icon"></i>取消</button>');
        arrHtml.push('</div>');
        dlg.Com("dlg-name", "<i class='fa fa-edit'></i> 修改密码", arrHtml.join(''), 450, function () { dlg.cls("dlg-name") });
    }

    function del() {
        model = $("#datagrid").jqxTreeGrid('getSelection')[0];
        if (model == undefined) {
            dlg.Error('对不起，请选中您要操作的记录！');
            return;
        }
        deleteRecord(model.UserId, model.UserName, '/sys/user/ajaxdel', function (data) {
            if (data) {
                dlg.Msg.Cor('删除成功！');
                model = undefined;
                $("#datagrid").jqxTreeGrid('updateBoundData');
            }
        });
    }
</script>